<%@page import="cn.sendto.service.UserInfoService"%>
<%@page import="cn.sendto.model.UserInfoModel"%>
<%@page import="cn.sendto.model.ResidentInfoModel"%>
<%@page import="java.util.List"%>
<%@page import="cn.sendto.service.ResidentInfoService"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" errorPage="err.jsp"%>
	<jsp:include page="lock.jsp"></jsp:include>
<%
	//设置编码方式
	request.setCharacterEncoding("UTF-8");
	//初始化用户类型
	String rank = "1";
	if (session.getAttribute("ranks") != null) {
		rank = session.getAttribute("ranks").toString();
	}
	//接收数据
	String userpass = request.getParameter("userpass");
	String usernewpass = request.getParameter("usernewpass");
	String comusernewpass = request.getParameter("comusernewpass");
	String username = "";

	//验证数据有效性
	ArrayList<Integer> errlist = new ArrayList<Integer>();
	if (userpass == null || userpass.equals("")) {
		errlist.add(1);
	}
	if (usernewpass == null || usernewpass.equals("")) {
		errlist.add(2);
	}
	if (comusernewpass == null || comusernewpass.equals("")) {
		errlist.add(3);
	}
	if (usernewpass == null || comusernewpass == null || usernewpass.equals("")
			|| !comusernewpass.equals(usernewpass)) {
		errlist.add(4);
	}

	//有无效数据返回
	if (errlist.size() > 0) {
		request.setAttribute("errinfo", errlist);
		request.getRequestDispatcher("alterinfo.jsp").forward(request, response);
		return;
	}
	//业主
	if (rank.equals("1")) {
		/* out.print(userpass+"a");
		out.print(usernewpass);
		out.print(comusernewpass); */
		ResidentInfoModel rim =(ResidentInfoModel) session.getAttribute("loginResident");
		username = rim.getUserName();
		ResidentInfoService ris = new ResidentInfoService();
		List<ResidentInfoModel> list = ris.doLogin(username, userpass);
		if(list.size() > 0 ){
			int i = ris.doAlterPass(usernewpass, username);
			if(i > 0){
			request.setAttribute("message", "<span style='color:red;'>"+"修改成功"+"</span>"+"，请用新密码登陆");
			rim.setUserPass(usernewpass);
			session.removeAttribute("loginResident");
			session.setAttribute("loginResident", rim);
			request.getRequestDispatcher("indexpass.jsp").forward(request, response);
			return;
			}else{
				request.setAttribute("message", "修改失败");
				request.getRequestDispatcher("indexpass.jsp").forward(request, response);
				return;
			}
		}else{
			request.setAttribute("message", "修改失败");
			request.getRequestDispatcher("indexpass.jsp").forward(request, response);
			return;
		} 
	}
	//管理员
	if (rank.equals("2")) {
		/* out.print(userpass+"b");
		out.print(usernewpass);
		out.print(comusernewpass); */
		UserInfoModel uim =(UserInfoModel) session.getAttribute("loginUser");
		username = uim.getUserName();
		UserInfoService uis = new UserInfoService();
		List<UserInfoModel> list = uis.doLogin(username, userpass);
		if(list.size() > 0){
			int i = uis.doAlterPass(usernewpass, username);
			if(i > 0){
				request.setAttribute("message", "修改成功，请用新密码登陆");
				uim.setUserPass(usernewpass);
				session.removeAttribute("loginUser");
				session.setAttribute("loginUser", uim);
				request.getRequestDispatcher("indexpass.jsp").forward(request, response);
				return;
				}else{
					request.setAttribute("message", "修改失败");
					request.getRequestDispatcher("indexpass.jsp").forward(request, response);
					return;
				}
			}else{
				request.setAttribute("message", "修改失败");
				request.getRequestDispatcher("indexpass.jsp").forward(request, response);
				return;
			}
	}
%>